package com.xfcycle.zebrabikegp.broadcast;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.xfcycle.zebrabikegp.assist.AlarmAssist;
import com.xfcycle.zebrabikegp.constant.BluetoothDeviceConstant;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光推送 推送信息广播接收者
 * Created by Wilson on 2018/1/3.
 */
public class JpushBroadcastReceiver extends BroadcastReceiver{
    private static final String TAG = "MyReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.i(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "接受到推送下来的自定义消息");
            receivingNotification(context,bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "接受到推送下来的通知");

            receivingNotification(context,bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.i(TAG, "用户点击打开了通知");
        } else {
            Log.i(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    private void receivingNotification(Context context, Bundle bundle){
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

        Log.i(TAG, "receivingNotification: 获取到数据  title "+ title + "  message  " + message + "   extras  " + extras);
        
        //判断是不是报警信息
//        if(){
        AlarmAssist.isAlarm = true;
        //发送报警信息
        Intent alarmInt = new Intent();
        alarmInt.setAction(BluetoothDeviceConstant.ALARM_MESSAGE_BROADCAST_RECEIVER_ACTION);
        context.sendBroadcast(alarmInt);
//        }
    }
}
